package com.jobsika.client.search;

import com.google.gwt.event.dom.client.MouseDownEvent;
import com.google.gwt.event.dom.client.MouseDownHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.http.client.URL;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.jobsika.client.Util;
import com.jobsika.domain.RPCPost;
import com.jobsika.domain.RPCUser;

public class SearchItem extends Composite {
	private AbsolutePanel skeleton;
	private VerticalPanel rightSkeleton;
	private RPCPost post;
	private RPCUser user;

	public SearchItem(RPCPost post) {
		this.post = post;
		skeleton = new AbsolutePanel();
		rightSkeleton = new VerticalPanel();

		Label title = new Label(post.getTitle());
		String postLocation = post.getLocation().replaceAll("-", ", ");
		postLocation = postLocation.replaceAll("_", " ");

		Label location = new Label(postLocation);
		Label date = new Label(Util.dtf.format(post.getDate()));

		rightSkeleton.add(title);
		rightSkeleton.add(location);
		rightSkeleton.add(date);
		skeleton.add(rightSkeleton, 16, 6);

		title.setStyleName("searchItem-title");
		location.setStyleName("searchItem-location");
		date.setStyleName("searchItem-date");
		skeleton.setStyleName("searchItem");

		this.addMouseOverHandler(mouseOver);
		this.addMouseOutHandler(mouseOut);
		this.addMouseDownHandler(postMouseDown);
		this.initWidget(skeleton);
	}
	
	public SearchItem(RPCUser user) {
		this.user = user;
		skeleton = new AbsolutePanel();
		rightSkeleton = new VerticalPanel();

		Label title = new Label(user.getFirstName()+" "+user.getLastName());
		Label location = new Label(user.getEmail());

		rightSkeleton.add(title);
		rightSkeleton.add(location);
		skeleton.add(rightSkeleton, 16, 6);

		title.setStyleName("searchItem-title");
		location.setStyleName("searchItem-location");
		skeleton.setStyleName("searchItem");

		this.addMouseOverHandler(mouseOver);
		this.addMouseOutHandler(mouseOut);
		this.addMouseDownHandler(userMouseDown);
		this.initWidget(skeleton);
	}

	private MouseOverHandler mouseOver = new MouseOverHandler() {
		@Override
		public void onMouseOver(MouseOverEvent event) {
			skeleton.addStyleName("searchItem-over");
		}
	};

	private MouseOutHandler mouseOut = new MouseOutHandler() {
		@Override
		public void onMouseOut(MouseOutEvent event) {
			skeleton.removeStyleName("searchItem-over");
		}
	};

	private MouseDownHandler postMouseDown = new MouseDownHandler() {
		@Override
		public void onMouseDown(MouseDownEvent event) {
			History.newItem("p="
					+ URL.encodeComponent(post.getUrl().toString()));
			History.fireCurrentHistoryState();
		}
	};
	
	private MouseDownHandler userMouseDown = new MouseDownHandler() {
		@Override
		public void onMouseDown(MouseDownEvent event) {
			History.newItem("u="+user.getEmail());
			History.fireCurrentHistoryState();
		}
	};

	public HandlerRegistration addMouseOverHandler(MouseOverHandler handler) {
		return addDomHandler(handler, MouseOverEvent.getType());
	}

	public HandlerRegistration addMouseOutHandler(MouseOutHandler handler) {
		return addDomHandler(handler, MouseOutEvent.getType());
	}

	public HandlerRegistration addMouseDownHandler(MouseDownHandler handler) {
		return addDomHandler(handler, MouseDownEvent.getType());
	}
}
